﻿using GameFramework.Localization;
using System;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace VRLauncher
{
    /// <summary>
    /// 流程 => 启动器。
    /// </summary>
    public class ProcedureLaunch : ProcedureBase
    {
        public override bool UseNativeDialog => true;

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            // 构建信息：发布版本时，把一些数据以 Json 的格式写入 Assets/GameMain/Configs/BuildInfo.txt，供游戏逻辑读取
            // GameEntry.BuiltinData.InitBuildInfo();

            // 语言配置：设置当前使用的语言，如果不设置，则默认使用操作系统语言
            InitLanguageSettings();

            // 声音配置：根据用户配置数据，设置即将使用的声音选项
            InitSoundSettings();

            //GameEntry.BuiltinData.InitBuildInfo();
            //string welcomeMessage = Utility.Text.Format("Hello! This is an empty project based on Game Framework {0}.", Version.GameFrameworkVersion);
            //Log.Info(welcomeMessage);
            //Log.Warning(welcomeMessage);
            //Log.Error(welcomeMessage);
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            // 运行一帧即切换到 Splash 展示流程
            ChangeState<ProcedureSplash>(procedureOwner);
        }
        private void InitLanguageSettings()
        {
            //if (GameEntry.Base.EditorResourceMode && GameEntry.Base.EditorLanguage != Language.Unspecified)
            //{
            //    // 编辑器资源模式直接使用 Inspector 上设置的语言
            //    return;
            //}

            Language language = GameManager.Localization.Language;
            if (GameManager.Setting.HasSetting(Constant.Setting.Language))
            {
                try
                {
                    string languageString = GameManager.Setting.GetString(Constant.Setting.Language);
                    language = (Language)Enum.Parse(typeof(Language), languageString);
                }
                catch
                {
                }
            }
           
            if (language != Language.English
                && language != Language.ChineseSimplified
                && language != Language.ChineseTraditional
                && language != Language.Korean)
            {
                // 若是暂不支持的语言，则使用英语
                language = Language.English;

                GameManager.Setting.SetString(Constant.Setting.Language, language.ToString());
                GameManager.Setting.Save();
            }

            GameManager.Localization.Language = language;
            Log.Info("Init language settings complete, current language is '{0}'.", language.ToString());
        }

        private void InitSoundSettings()
        {
            GameManager.Sound.Mute("Music", GameManager.Setting.GetBool(Constant.Setting.MusicMuted, false));
            GameManager.Sound.SetVolume("Music", GameManager.Setting.GetFloat(Constant.Setting.MusicVolume, 0.3f));
            GameManager.Sound.Mute("Sound", GameManager.Setting.GetBool(Constant.Setting.SoundMuted, false));
            GameManager.Sound.SetVolume("Sound", GameManager.Setting.GetFloat(Constant.Setting.SoundVolume, 1f));
            GameManager.Sound.Mute("UISound", GameManager.Setting.GetBool(Constant.Setting.UISoundMuted, false));
            GameManager.Sound.SetVolume("UISound", GameManager.Setting.GetFloat(Constant.Setting.UISoundVolume, 1f));
            Log.Info("Init sound settings complete.");
        }

    }
}
